<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\TypeModel;
use App\TypeTableMode;
use Illuminate\Http\Request;

class TypesTableController extends Controller
{
    public function types(Request $request)
    {

        $types = TypeTableMode::get();
        return view("admin.types", compact("types"));
    }


    public function page(Request $request)
    {
        return view("admin.page");
    }


    public function base(Request $request)
    {

        return view("admin.base");
    }


    public function typesaddpage(Request $request)
    {
        $id = $request->input("id");
        $type =TypeTableMode::find($id);
//        $type = TypeModel::where("id",$id)->first();
        return view("admin.typesaddpage", compact("type"));
    }



    public function insert(Request $request)
    {
        $data = $request->except("_token");
        if ($data['id']==0) {
            $type = TypeTableMode::create($data);
            $result=['title'=>"数据添加成功！","err"=>0];
        } else {
            $type=TypeTableMode::find($data['id']);
            $type->update($data);
            $result=['title'=>"数据编辑成功！","err"=>0];
        }
        $result=json_encode($result);
        return $result;
    }

//    public function insert(Request $request)
//    {
//        $data = $request->except("_token");
//        if ($data['id'] == 0) {
//            TypeTableMode::create($data);
//            $result = ["title"=>"添加数据成功！","err"=>0];
//        } else {
//            $type = TypeTableMode::find($data['id']);
//            $type->update($data);
//            $result = ["title"=>"数据编辑成功！","err"=>0];
//        }
//        $result = json_encode($result);
//        return $result;
//    }

//    public function delete(Request $request)
//    {
//        $id = $request->input("id");
//        TypeTableMode::where("id",$id)->delete();
//        return redirect("types");
//    }


    public function delete(Request $request)
    {
        $id = $request->input("id");
        TypeTableMode::where("id",$id)->delete();
        $result = ["title"=>"删除成功！","err"=>0];
        $result = json_encode($result);
        return $result;
    }
}
